<?php

namespace App\Admin\Http\Controllers\Business;


use App\Admin\Eloquent\ProjectTypeTemplate;
use App\Admin\Eloquent\ProjectTypeTemplateAttribute;
use App\Admin\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Laracasts\Flash\Flash;

class ProjectTypeTemplateAttributesController extends Controller
{
    public function index(int $template_id)
    {
        $project_type_template_attributes = ProjectTypeTemplateAttribute::where('project_type_template_id', $template_id)->paginate(15);

        $links = $project_type_template_attributes->render();

        return view('admin.business.project_type_template_attribute.index', compact('template_id', 'query', 'links', 'project_type_template_attributes'));
    }

    public function create(Request $request, int $template_id)
    {
        $type = $request->only('type');


        if ($type['type'] == 0) {
            return view('admin.business.project_type_template_attribute.create', compact('template_id'));
        } else if ($type['type'] == 1) {
            return view('admin.business.project_type_template_attribute.create_1', compact('template_id'));
        } else {
            return abort(404);
        }
    }

    public function store(Request $request, int $template_id)
    {

        $args = $request->input();

        $args['project_type_template_id'] = $template_id;

        $project_type_template_attribute = new ProjectTypeTemplateAttribute($args);

        $project_type_template_attribute->save();

        Flash::success('保存模板属性成功!');

        return $this->redirectAction('Business\ProjectTypeTemplateAttributesController@index', compact('template_id'));

    }

    public function edit(int $template_id, int $id)
    {
        $project_type_template_attribute = ProjectTypeTemplateAttribute::findOrFail($id);

        if ($project_type_template_attribute->template_attribute == 0) {
            return view('admin.business.project_type_template_attribute.edit', compact('template_id', 'project_type_template_attribute'));
        } else if ($project_type_template_attribute->template_attribute == 1) {
            return view('admin.business.project_type_template_attribute.edit_1', compact('template_id', 'project_type_template_attribute'));
        } else {
            return abort(404);
        }

    }

    public function update(Request $request, int $template_id, int $id)
    {
        $project_type_template_attribute = ProjectTypeTemplateAttribute::findOrFail($id);

        $project_type_template_attribute->update($request->input());

        Flash::success("更新一条模板属性成功!");

        return $this->redirectAction('Business\ProjectTypeTemplateAttributesController@index', compact('template_id'));
    }

    public function destroy(int $template_id, int $id)
    {
        $project_type_template_attribute = ProjectTypeTemplateAttribute::findOrFail($id);

        $project_type_template_attribute->delete();

        Flash::success('删除模板属性成功!');

        return $this->redirectAction('Business\ProjectTypeTemplateAttributesController@index', compact('template_id'));
    }
}